﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DocumentTermListOutput:ResultBase
    {
        #region props
        private int _DocCount;
        /// <summary></summary>
        public int DocCount
        {
            get
            {
                return this._DocCount;
            }
            set
            {
                this._DocCount = value;
            }
        }

        private string _DocTermListResultFilePath;

        /// <summary>
        /// <Docs>
        ///     <Doc DocID="0" FilePath=""/>
        /// </Docs>
        /// </summary>
        public string DocTermListResultFilePath
        {
            get
            {
                return this._DocTermListResultFilePath;
            }
            set
            {
                this._DocTermListResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public DocumentTermListOutput():base(Guid.Empty,Guid.Empty)
        {
            this._DocCount = 0;
            this._DocTermListResultFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="docCount"></param>
        /// <param name="docListResultFilePath"></param>
        public DocumentTermListOutput(Guid componentID, Guid jobTicketID, 
            int docCount, string docListResultFilePath)
            : base(componentID, jobTicketID)
        {
            this._DocCount = docCount;
            this._DocTermListResultFilePath = docListResultFilePath;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                DocumentTermListOutput output = new DocumentTermListOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.DocCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "DocCount", "0"));
                output.DocTermListResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "DocListFilePath", "");

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"DocCount",this.DocCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"DocListFilePath",this.DocTermListResultFilePath);

            return xNode;
        }
    }
}
